//
//  YCFillApplyInfoCell.m
//  YCSchool
//
//  Created by gmall on 16/5/4.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//  填写报名信息tableCell

#import "YCFillApplyInfoCell.h"
#import "YCPlaceholderTextView.h"

@interface YCFillApplyInfoCell()<UITextViewDelegate>
/** leftLabel  */
@property (nonatomic, strong) UILabel *leftLabel;
/** 输入框  */
@property (nonatomic, weak) YCPlaceholderTextView *textView;
/** 发送验证码  */
@property (nonatomic, weak) UIButton *sendButton;


@end

@implementation YCFillApplyInfoCell


+ (instancetype)tabelViewWithTableView:(UITableView *)tableView withIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier = @"fillApplyInfoCell";
    YCFillApplyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[YCFillApplyInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self makeSubView];
    }
    return self;
}


- (void)makeSubView
{
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.font = [UIFont systemFontOfSize:14];
    leftLabel.text = @"名字";
    leftLabel.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:leftLabel];
    self.leftLabel = leftLabel;
    
    YCPlaceholderTextView *textView = [[YCPlaceholderTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:14];
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.textContainer.maximumNumberOfLines = 1;//只可显示一行
    textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
    textView.returnKeyType = UIReturnKeyDone;
    UIView *keyBoardTopLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];//数字键盘上面没有一条线,故加一条
    keyBoardTopLine.backgroundColor = [UIColor yc_colorWithUInt:0x808080 alpha:0.3];
    textView.inputAccessoryView = keyBoardTopLine;
    textView.placeholder = @"请输入对应的内容";
    [self.contentView addSubview:textView];
    _textView = textView;
    
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendButton.layer.cornerRadius = 10;
    sendButton.layer.masksToBounds = YES;
    sendButton.titleLabel.font = [UIFont systemFontOfSize:iPhone6?12:10];
    sendButton.backgroundColor = [UIColor yc_colorWithUInt:0x3ba4fa];
    [sendButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sendButton addTarget:self action:@selector(didTouchDownSendCodeButton:) forControlEvents:UIControlEventTouchDown];//按下
    [sendButton addTarget:self action:@selector(didTouchUpinsideSendCodeButton:) forControlEvents:UIControlEventTouchUpInside];//松开
    [self.contentView addSubview:sendButton];
    self.sendButton = sendButton;
    
    CGSize textViewSize = [self.textView.text yc_sizeWithFont:textView.font MaxSize:CGSizeMake(CGFLOAT_MAX, 0)];
    
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.centerY.equalTo(self.contentView);
    }];

    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(widthFromIphone6Width(142));
        make.right.equalTo(self.contentView).offset(-15);
        make.centerY.equalTo(self.contentView);
        make.height.equalTo(@(textViewSize.height + 8));
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.equalTo(self.contentView).offset(-15);
        make.width.equalTo(@(heightFromIphone6Height(77)));
        make.height.equalTo(@27);
    }];
    
}

- (void)setModel:(YCFillApplyModel *)model
{
    _model = model;
    
    if (model.isSend) {
        self.sendButton.hidden = NO;
    } else {
        self.sendButton.hidden = YES;
    }
    if (model.isNumberKeyBoard) {
        self.textView.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        self.textView.keyboardType = UIKeyboardTypeDefault;
    }
    
    CGSize textViewSize = [self.textView.text yc_sizeWithFont:self.textView.font MaxSize:CGSizeMake(CGFLOAT_MAX, 0)];
    if (![model.name isEqualToString:@"取件地址"]) {
        
        self.textView.textContainer.maximumNumberOfLines = 1;//只可显示一行
        self.textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(textViewSize.height + 8));
        }];
    } else {
        self.textView.textContainer.maximumNumberOfLines = 2;//2行
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(textViewSize.height * 2 + 8));
        }];
    }
    
    self.leftLabel.text = model.name;
    self.textView.placeholder = model.placeholder;
}

- (void)didTouchUpinsideSendCodeButton:(UIButton *)sendButton
{
    YCLog(@"发送验证码按钮---松开");
    self.sendButton.backgroundColor = [UIColor yc_colorWithUInt:0x3ba4fa alpha:1];
    [SVProgressHUD showSuccessWithStatus:@"发送验证码成功"];
}

- (void)didTouchDownSendCodeButton:(UIButton *)sendButton
{
    YCLog(@"发送验证码按钮---刚按下");
    self.sendButton.backgroundColor = [UIColor yc_colorWithUInt:0x3ba4fa alpha:0.5];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITextViewDelegate 代理方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
// 结束输入时
- (void)textViewDidEndEditing:(UITextView *)textView
{
    YCLog(@"%@",textView.text);
    self.model.contentText = textView.text;
    if ([_delegate respondsToSelector:@selector(fillApplyInfoCellEndEdit:)]) {
        [_delegate fillApplyInfoCellEndEdit:self];
    }
}

@end
